package com.cloud.drore.eboss.operation.service.impl;

import com.cloud.drore.eboos.common.base.PagePo;
import com.cloud.drore.eboos.common.base.ResultMessage;
import com.cloud.drore.eboos.common.constant.CommonConstant;
import com.cloud.drore.eboos.common.entity.dealer.DistributorProductQueryPo;
import com.cloud.drore.eboos.common.entity.dealer.DistributorProductQueryVo;
import com.cloud.drore.eboos.exception.BusinessException;

import com.cloud.drore.eboss.operation.mapper.CooperationProductMapper;
import com.cloud.drore.eboss.operation.service.ICooperationProductService;
import com.cloud.drore.eboss.supplier.entity.ProductValidateMethod;
import com.cloud.drore.eboss.supplier.mapper.ProductValidateMethodMapper;
import com.cloud.drore.eboss.supplier.service.impl.CombinationProductServiceImpl;
import com.cloud.drore.eboss.util.DictionaryMapUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 浙江卓锐科技股份有限公司 版权所有  Copyright 2018<br>
 *
 * @Description: <br>
 * @Project: eboos <br>
 * @CreateDate: Created in 2018/3/18 17:33 <br>
 * @Author: <a href="1451173193@qq.com">杨亮</a>
 */
@Service
public class CooperationProductServiceImpl implements ICooperationProductService {
    @Resource
    private CooperationProductMapper cooperationProductMapper;
    @Resource
    private CombinationProductServiceImpl combinationProductService;
    @Resource
    private ProductValidateMethodMapper productValidateMethodMapper;

    /**
     * @param pagePo
     * @ClassName: CooperationProductServiceImpl
     * @Return: java.util.List<com.cloud.drore.eboss.operation.entity.vo.DistributorProductQueryVo>
     * @Decription:分销商模块获取组合产品信息
     * @CreateDate: Created in 2018/3/18 17:35
     * @Author: <a href="1451173193@qq.com">杨亮</a>
     * @Modify:
     */
    @Override
    public ResultMessage<List<DistributorProductQueryVo>> findCombinationProductByParam(PagePo<DistributorProductQueryPo> pagePo) throws BusinessException {
        PageHelper.startPage(pagePo);
        DistributorProductQueryPo po = pagePo.getFields();
        if (po == null || po.getDistributorId() == null) {
            throw new BusinessException("分销商id不能为空！");
        }
        List<DistributorProductQueryVo> list = cooperationProductMapper.findCombinationProductByParam(po);
        Map<String, String> crowdMap = DictionaryMapUtil.getMapByParentCode(CommonConstant.PRODUCT_CROWD);
        list.stream().map(item -> {
            item.setProductCrowd(crowdMap.get(item.getProductCrowd()));
        //验证方式查询
        //20180413暂时不用
/*            List<String> validateMethodDbCode = null;
            try {
                validateMethodDbCode = combinationProductService.selectCombinationProductById(item.getId()).getVerificationType();
            } catch (BusinessException e) {
                e.printStackTrace();
            }
            item.setVerificationType(validateMethodDbCode);*/
            return item;
        }).collect(Collectors.toList());
        PageInfo pageInfo = new PageInfo(list);
        return ResultMessage.success(pageInfo);
    }

    /**
     * @param pagePo
     * @ClassName: CooperationProductServiceImpl
     * @Return: java.util.List<com.cloud.drore.eboss.operation.entity.vo.DistributorProductQueryVo>
     * @Decription:分销商模块获取门票产品信息
     * @CreateDate: Created in 2018/3/18 17:35
     * @Author: <a href="1451173193@qq.com">杨亮</a>
     * @Modify:
     */
    @Override
    public ResultMessage<List<DistributorProductQueryVo>> findTicketProductByParam(PagePo<DistributorProductQueryPo> pagePo) throws BusinessException {
        PageHelper.startPage(pagePo);
        DistributorProductQueryPo po = pagePo.getFields();
        if (po == null || po.getDistributorId() == null) {
            throw new BusinessException("分销商id不能为空！");
        }
        List<DistributorProductQueryVo> list = cooperationProductMapper.findTicketProductByParam(po);
        Map<String, String> crowdMap = DictionaryMapUtil.getMapByParentCode(CommonConstant.PRODUCT_CROWD);
       // Map<String, String> validateMap = DictionaryMapUtil.getMapByParentCode(CommonConstant.VERIFYING_CODE_TYPE);
        list.stream().map(item -> {
            item.setProductCrowd(crowdMap.get(item.getProductCrowd()));
            //20180413暂时不用
            //验证方式查询
           /* ProductValidateMethod productValidateMethod = new ProductValidateMethod();
            productValidateMethod.setProductId(item.getId());
            productValidateMethod.setProductType(CommonConstant.TYPE_OF_TICKET);
            productValidateMethod.setState("1");
            List<ProductValidateMethod> validateMethodList = productValidateMethodMapper.selectValidateMethodByParam(productValidateMethod);
            List<String> validateMethodDbCode = validateMethodList.stream().map(ProductValidateMethod::getDicCode).collect(Collectors.toList());
            validateMethodDbCode = validateMethodDbCode.stream().map(ite -> validateMap.get(ite)).collect(Collectors.toList());
            item.setVerificationType(validateMethodDbCode);*/
            return item;
        }).collect(Collectors.toList());
        PageInfo pageInfo = new PageInfo(list);
        return ResultMessage.success(pageInfo);
    }
}
